Skip to content

IDBDatabase: make the upgrade example work for existing databases - #45955

Open
AsifMahsud wants to merge 2 commits into
mdn:mainfrom
AsifMahsud:idbdatabase-upgrade-example
Open

AsifMahsud wants to merge 2 commits into
mdn:mainfrom
AsifMahsud:idbdatabase-upgrade-example

Conversation

@AsifMahsud

Copy link
Copy Markdown
Contributor

Description

Reworks the onupgradeneeded handler in the IDBDatabase example so it handles upgrades from an older version, not just first-time creation:

  • The handler now checks event.oldVersion and applies each upgrade step the user hasn't had yet. Version 1 creates the toDoList store and its date/time indexes; version 2 adds the notified index to the existing store, which it gets from the upgrade transaction (DBOpenRequest.transaction.objectStore()) instead of calling createObjectStore() again.
  • The open() call asks for version 2 to match.
  • Fixes a node.appendChild typo (the variable is note), plus a missing full stop in a comment.

Motivation

As the issue says, the old handler always called createObjectStore("toDoList"), so it only worked when the database didn't exist yet. Bumping the version for anyone with an existing database threw a ConstraintError and aborted the upgrade. I checked this with fake-indexeddb: after creating the database at version 1, the old handler fails on the upgrade to version 2 with ConstraintError, while the new handler produces the same six indexes both for a fresh install at version 2 and for an upgrade from version 1.

Additional details

IndexedDB spec, IDBVersionChangeEvent.oldVersion (0 for a new database) and IDBOpenDBRequest.transaction during an upgrade.

Related issues and pull requests

Fixes #26638

@AsifMahsud
AsifMahsud requested a review from a team as a code owner October 2, 2026 11:19
@AsifMahsud
AsifMahsud requested review from wbamberg and removed request for a team October 2, 2026 11:19
@github-actions github-actions Bot added Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed labels Oct 2, 2026

@Josh-Cena Josh-Cena left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice. Thank you!

@Josh-Cena

Copy link
Copy Markdown
Member

Holding this until mdn/dom-examples#404 gets merged. And then we'll update all the other pages too.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor
Preview URLs (52 pages)
External URLs (52)

URL: /en-US/docs/Web/API/IDBDatabase
Title: IDBDatabase


URL: /en-US/docs/Web/API/IDBDatabase/close
Title: IDBDatabase: close() method


URL: /en-US/docs/Web/API/IDBDatabase/close_event
Title: IDBDatabase: close event


URL: /en-US/docs/Web/API/IDBDatabase/createObjectStore
Title: IDBDatabase: createObjectStore() method


URL: /en-US/docs/Web/API/IDBDatabase/name
Title: IDBDatabase: name property


URL: /en-US/docs/Web/API/IDBDatabase/objectStoreNames
Title: IDBDatabase: objectStoreNames property


URL: /en-US/docs/Web/API/IDBDatabase/transaction
Title: IDBDatabase: transaction() method


URL: /en-US/docs/Web/API/IDBDatabase/version
Title: IDBDatabase: version property


URL: /en-US/docs/Web/API/IDBDatabase/versionchange_event
Title: IDBDatabase: versionchange event


URL: /en-US/docs/Web/API/IDBFactory
Title: IDBFactory


URL: /en-US/docs/Web/API/IDBFactory/open
Title: IDBFactory: open() method


URL: /en-US/docs/Web/API/IDBObjectStore
Title: IDBObjectStore


URL: /en-US/docs/Web/API/IDBObjectStore/add
Title: IDBObjectStore: add() method


URL: /en-US/docs/Web/API/IDBObjectStore/autoIncrement
Title: IDBObjectStore: autoIncrement property


URL: /en-US/docs/Web/API/IDBObjectStore/clear
Title: IDBObjectStore: clear() method


URL: /en-US/docs/Web/API/IDBObjectStore/createIndex
Title: IDBObjectStore: createIndex() method


URL: /en-US/docs/Web/API/IDBObjectStore/delete
Title: IDBObjectStore: delete() method


URL: /en-US/docs/Web/API/IDBObjectStore/deleteIndex
Title: IDBObjectStore: deleteIndex() method


URL: /en-US/docs/Web/API/IDBObjectStore/get
Title: IDBObjectStore: get() method


URL: /en-US/docs/Web/API/IDBObjectStore/indexNames
Title: IDBObjectStore: indexNames property


URL: /en-US/docs/Web/API/IDBObjectStore/keyPath
Title: IDBObjectStore: keyPath property


URL: /en-US/docs/Web/API/IDBObjectStore/name
Title: IDBObjectStore: name property


URL: /en-US/docs/Web/API/IDBObjectStore/put
Title: IDBObjectStore: put() method


URL: /en-US/docs/Web/API/IDBObjectStore/transaction
Title: IDBObjectStore: transaction property


URL: /en-US/docs/Web/API/IDBOpenDBRequest
Title: IDBOpenDBRequest


URL: /en-US/docs/Web/API/IDBOpenDBRequest/blocked_event
Title: IDBOpenDBRequest: blocked event


URL: /en-US/docs/Web/API/IDBOpenDBRequest/upgradeneeded_event
Title: IDBOpenDBRequest: upgradeneeded event


URL: /en-US/docs/Web/API/IDBRequest
Title: IDBRequest


URL: /en-US/docs/Web/API/IDBRequest/error
Title: IDBRequest: error property


URL: /en-US/docs/Web/API/IDBRequest/error_event
Title: IDBRequest: error event


URL: /en-US/docs/Web/API/IDBRequest/readyState
Title: IDBRequest: readyState property


URL: /en-US/docs/Web/API/IDBRequest/result
Title: IDBRequest: result property


URL: /en-US/docs/Web/API/IDBRequest/source
Title: IDBRequest: source property


URL: /en-US/docs/Web/API/IDBRequest/success_event
Title: IDBRequest: success event


URL: /en-US/docs/Web/API/IDBRequest/transaction
Title: IDBRequest: transaction property


URL: /en-US/docs/Web/API/IDBTransaction
Title: IDBTransaction


URL: /en-US/docs/Web/API/IDBTransaction/abort
Title: IDBTransaction: abort() method


URL: /en-US/docs/Web/API/IDBTransaction/abort_event
Title: IDBTransaction: abort event


URL: /en-US/docs/Web/API/IDBTransaction/commit
Title: IDBTransaction: commit() method


URL: /en-US/docs/Web/API/IDBTransaction/complete_event
Title: IDBTransaction: complete event


URL: /en-US/docs/Web/API/IDBTransaction/db
Title: IDBTransaction: db property


URL: /en-US/docs/Web/API/IDBTransaction/error
Title: IDBTransaction: error property


URL: /en-US/docs/Web/API/IDBTransaction/error_event
Title: IDBTransaction: error event


URL: /en-US/docs/Web/API/IDBTransaction/mode
Title: IDBTransaction: mode property


URL: /en-US/docs/Web/API/IDBTransaction/objectStore
Title: IDBTransaction: objectStore() method


URL: /en-US/docs/Web/API/IDBVersionChangeEvent
Title: IDBVersionChangeEvent


URL: /en-US/docs/Web/API/IDBVersionChangeEvent/newVersion
Title: IDBVersionChangeEvent: newVersion property


URL: /en-US/docs/Web/API/IndexedDB_API/Checking_when_a_deadline_is_due
Title: Checking when a deadline is due


URL: /en-US/docs/Web/API/Notification/icon
Title: Notification: icon property


URL: /en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API
Title: Using the Notifications API


URL: /en-US/docs/Web/API/Window/indexedDB
Title: Window: indexedDB property


URL: /en-US/docs/Web/API/WorkerGlobalScope/indexedDB
Title: WorkerGlobalScope: indexedDB property

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

leading the developer down a dead end in onupgradeneeded

2 participants